//
//  MyDocument.m
//  BookMaker
//
//  Created by Mathieu Tozer on 22/11/07.
//  Copyright plasq 2007 . All rights reserved.
//

#import "MyDocument.h"
#import "CoreData+Additions.h"
#import "Book.h"
#import "Exporter.h"

@implementation MyDocument

- (id)init 
{
    self = [super init];
    if (self != nil) {
        // initialization code
    }
    return self;
}

- (NSString *)windowNibName 
{
    return @"MyDocument";
}


- (Book *)book {
    return [[_book retain] autorelease];
}

- (void)setBook:(Book *)value {
    if (_book != value) {
        [_book release];
		[value retain];
        _book = value;
    }
}

- (void)bookExportLocationChooserDidEnd: (NSOpenPanel *)openPanel
					   returnCode: (int)returnCode
					  contextInfo: (void *)book
{
    if (returnCode == NSOKButton) {
		NSArray *filenames = [openPanel filenames];
		if ([filenames count] >= 1) {
			Exporter *exporter = [Exporter exporterToExportBook:(Book *)book];
			[exporter export:@"pdf" toPath:[filenames objectAtIndex:0]];
		}
	}
}

- (void)chooseLocationForExportBook:(id)book
{
	NSOpenPanel *panel = [NSOpenPanel openPanel];
    [panel setCanChooseDirectories: YES];
    [panel setCanChooseFiles: NO];
	[panel setCanCreateDirectories:YES];
	[panel setAllowsMultipleSelection:NO];
	
    [panel beginSheetForDirectory:nil
							 file: nil
							types: nil
				   modalForWindow: [self windowForSheet]
					modalDelegate: self
				   didEndSelector: @selector(bookExportLocationChooserDidEnd:returnCode:contextInfo:)
					  contextInfo: book];
}

- (IBAction)exportToPDFTheBook:(Book *)book
{
	[self chooseLocationForExportBook:book];
}



- (void)windowControllerDidLoadNib:(NSWindowController *)windowController 
{
    [super windowControllerDidLoadNib:windowController];
    // user interface preparation code
	//create a book object for this document, if there isn't one already
	NSArray *results = [[self managedObjectContext] executeFetchRequestWithEntity:@"Book" andPredicate:nil error:nil];
	if (results && [results count] < 1) {
		//create a new one
		NSManagedObject *book = [[self managedObjectContext] insertNewObjectOfEntity:@"Book"];
		[book setValue:@"New Book" forKey:@"name"];
		[self setBook:(Book *)book];
	} else {
		//there is one already
		[self setBook:[results objectAtIndex:0]];
	}
}


- (void)bookLocationChooserDidEnd: (NSOpenPanel *)openPanel
				  returnCode: (int)returnCode
				 contextInfo: (void *)book
{
    if (returnCode == NSOKButton) {
		NSArray *filenames = [openPanel filenames];
		if ([filenames count] >= 1) {
			[(Book *)book setValue:[[filenames objectAtIndex:0] stringByAbbreviatingWithTildeInPath] forKey:@"path"];
		}
	}
}

- (void)chooseDirectoryForBook:(id)book
{
	NSOpenPanel *panel = [NSOpenPanel openPanel];
    [panel setCanChooseDirectories: YES];
    [panel setCanChooseFiles: NO];
	[panel setCanCreateDirectories:YES];
	[panel setAllowsMultipleSelection:NO];
	
    [panel beginSheetForDirectory:nil
							 file: nil
							types: nil
				   modalForWindow: [self windowForSheet]
					modalDelegate: self
				   didEndSelector: @selector(bookLocationChooserDidEnd:returnCode:contextInfo:)
					  contextInfo: book];
}


@end
